package br.com.pibjp.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

public class EntityManagerSingleton {
	
	private EntityManager entityManager;
	private static EntityManagerSingleton instance;
	
	private EntityManagerSingleton(){
		
	}
	
	public static EntityManagerSingleton getInstance(){
		if (instance == null){
			instance = new EntityManagerSingleton();
		}
		
		return instance;
	}
	
	public EntityManager getEntityManager(EntityManagerFactory factory){
		if(this.entityManager == null || !this.entityManager.isOpen()){
			this.entityManager = factory.createEntityManager();
		}
		
		return entityManager;
	}
}
